<?
include_once("./qpass_config.php");
include_once("$gnuPath/common.php");

include_once("./lib_qpass_inc.php");
admin_check();

${"g".$gnuVersion}['title'] = "Qpass 소분류 관리";  
include_once("./head_qpass.php");
 

if($small_code) {

	$big_code = substr($small_code, 0,2);
	$mid_code = substr($small_code, 0,4);
}




if($mode == 'add') {

	$sql = "select count(*) from qpass_catSmall where small_code = '$small_code_new' ";

	
	$result_count = mysql_query($sql);
	$cat_count = mysql_result($result_count, 0,0);
	

	if($cat_count > 0) {

		  echo "<script>alert('코드 $small_code 은(는) 이미 생성된 코드이니, 목록에서 처리하세요.');</script>";
	}
	else {

		  $sql = "insert into qpass_catSmall (small_code, small_name, view_index) values ('$small_code_new', '$small_name', '$view_index')";
		  /*echo $sql;*/
		  
		  mysql_query($sql);
	}
}

else if($mode == 'modify' && $small_code) {

	$sql = "update qpass_catSmall set small_name = '$small_name', view_index = '$view_index'  where small_code = '$small_code' ";
	/*echo $sql;*/

	mysql_query($sql);
}

else if($mode == 'delete' && $small_code) {


	$sql = "delete from qpass_catSmall where small_code = '$small_code' ";
	/*echo $sql;*/

	mysql_query($sql);
}

?>


<script type='text/javascript'> 

function code_check(code, next_field) {


    if( isNaN(code.value) ) {
	
	      alert("코드는 숫자만 가능합니다. (10~99)");  

		  code.value = '';
		  code.focus();
          return;
    }



    if( code.value.length == 1 && code.value == 0 ) {
	
	      alert("코드는 0 으로 시작하지 않아야 합니다.");  

		  code.value = '';
		  code.focus();
          return;
    }



	if(code.value.length == 2) next_field.focus();

}




function add_check(f, mid_code) {


    if( f.small_code_short.value.length < 2 ) {
	
	      alert("코드는 두자리수로 입력하세요.");  

		  f.small_code_short.focus();
          return;
    }


    if( f.small_name.value == '' ) {
	
	      alert("분류명을 입력하세요.");  

		  f.small_name.focus();
          return;
    }



	f.mode.value = 'add';
	f.small_code_new.value = mid_code + f.small_code_short.value;

	f.submit();
    
}



function small_modify(big_code, mid_code, small_code) {

	var f = document.send_form;


	f.big_code.value = big_code;
	f.mid_code.value = mid_code;
	f.small_code.value = small_code;

	f.small_name.value = document.getElementById('name_'+small_code).value;
	f.view_index.value = document.getElementById('view_'+small_code).value;


	f.mode.value = 'modify';
	f.submit();
}



function small_delete(code) {

	var f = document.send_form;

	/*if( !confirm("이 소분류를 삭제하시면, 이 소분류와 연결된 시험들과 문제들의 정보도 함께 삭제됩니다.정말 삭제합니까?") ) return;*/

	if( !confirm("삭제할까요?") ) return;
	f.small_code.value = code;

	f.mode.value = 'delete';
	f.submit();
}


</script>





<form name='send_form' method='post' style='margin:0' action='<?=$PHP_SELF?>'>

<input type='hidden' name='mode' value=''>

<input type='hidden' name='big_code' value=''>
<input type='hidden' name='mid_code' value=''>
<input type='hidden' name='small_code' value=''>

<input type='hidden' name='small_name' value=''>
<input type='hidden' name='view_index' value=''>

</form>




<form name='cat_form' method='post' style='margin:0' action='<?=$PHP_SELF?>'>
<input type='hidden' name='mode' value=''>
<input type='hidden' name='small_code_new' value=''>


<p align='center' style='margin:20px 0 0 0; font-weight:bold;'>소분류 목록</p>


<table align='center' width='800' cellspacing='1' cellpadding='5' style='margin: 10px 0 50px 0; background-color:#ccc' border='0'>
<tr style='background-color:white'> 
	  
<td align='center' width='120'>대분류</td> 
<td align='center' width='170'>중분류</td>
<td align='center' width='190'>소분류</td>
<td align='center' width=''>소분류 처리</td> 
    
</tr>  


<tr style='background-color:white'> 
	  
<td align='center'>

      <select name='big_code' size='10' style='width:98%' onchange="this.form.mid_code.value = ''; this.form.small_code.value = ''; this.form.submit()">
<?

  $sql = "select * from qpass_catBig order by big_code";
  $result_big = mysql_query($sql);


  while( $row_big = mysql_fetch_array($result_big) ) {

	    if($big_code == '') $big_code = $row_big[big_code];  /* 코드가 정해지지 않았으면 첫번째 대분류를 디폴트로 설정.*/

        if($big_code == $row_big[big_code]) {
		
			  echo "<option value='$row_big[big_code]' selected>[{$row_big[big_code]}] $row_big[big_name]</option>";
			  $big_name = $row_big[big_name];
		}
	    else 
			  echo "<option value='$row_big[big_code]'>[{$row_big[big_code]}] $row_big[big_name]</option>";
  }
?>
      </select>
</td> 



<td align='center'>

      <select name='mid_code' size='10' style='width:98%' onchange=" this.form.small_code.value = ''; this.form.submit()"'>
<?

  $sql = "select * from qpass_catMid where left(mid_code, 2) = '$big_code' order by mid_code";


  $result_mid = mysql_query($sql);


  while( $row_mid = mysql_fetch_array($result_mid) ) {

	    if($mid_code == '') $mid_code = $row_mid[mid_code];  /* 코드가 정해지지 않았으면 첫번째 중분류를 디폴트로 설정.*/

        if($mid_code == $row_mid[mid_code]) {

			echo "<option value='$row_mid[mid_code]' selected>[{$row_mid[mid_code]}] $row_mid[mid_name]</option>";
			$mid_name = $row_mid[mid_name];
		}
	    else 
			echo "<option value='$row_mid[mid_code]'>[{$row_mid[mid_code]}] $row_mid[mid_name]</option>";

  }



?>
      </select>


</td>



<td align='center'>

      <select name='small_code' size='10' style='width:98%' onchange='this.form.submit()'>
<?

  $sql = "select * from qpass_catSmall where small_code like '{$mid_code}__' order by small_code";


  $result_small = mysql_query($sql);


  while( $row_small = mysql_fetch_array($result_small) ) {

	    if($small_code == '') $small_code = $row_small[small_code];  /* 코드가 정해지지 않았으면 첫번째 중분류를 디폴트로 설정.*/

        if($small_code == $row_small[small_code]) {

			echo "<option value='$row_small[small_code]' selected>[".$row_small[view_index]."] $row_small[small_name]</option>";
			$small_name = $row_small[small_name];
			$view_index = $row_small[view_index];
		}
	    else 
			echo "<option value='$row_small[small_code]'>[".$row_small[view_index]."] $row_small[small_name]</option>";


		$next_code = substr($row_small[small_code], 4,2) + 1;
  }

  if($next_code == '') $next_code = 10;

  $next_index = $next_code - 9;
?>
      </select>
</td> 




<td align='center' valign='top'>


<?if($mid_name) {?>


      <table  width='100%' cellspacing='0' cellpadding='1' style='margin:7px 0 15px 0;' border='0'>

      <tr height='30'> 
	  
      <td align='left'>코드 :</td> 
      <td align='left'><?=$small_code?>
	  

	  &nbsp; <a href="./admin_exam.php?small_code=<?=$small_code?>" title='시험관리페이지'><img src='./img/btn_exam.gif' width='27' height='20' align='absmiddle' style=''></a>

	  <a href="./admin_question.php?small_code=<?=$small_code?>" title='문제관리페이지'><img src='./img/btn_question.gif' width='27' height='20' align='absmiddle'></a>
	  
	  
	  </td> 
   
      </tr>  


      <tr> 
      <td align='left' width='50'>분류명 : </td>
	  
      <td align='left'><input type='text' id='name_<?=$small_code?>' maxlength='100' value='<?=$small_name?>' class='ed' style='width:95%' onclick="this.style.color='blue';"></td>

      </tr>  

      <tr> 
      <td align='left' width='60'>목록순서 : </td>
	  
      <td align='left'><input type='text' id='view_<?=$small_code?>' value='<?=$view_index?>' class='ed' style='width:35px; text-align:center;' onclick="this.style.color='blue';"> (0 이면 홈페이지에 표시안됨)</td>

    
      </tr>  
	  </table>




	  <p align='center'><input type='button' value='수정' onclick="small_modify('<?=$big_code?>', '<?=$mid_code?>', '<?=$small_code?>')" onfocus='this.blur()' class="btn02_qpass"> <input type='button' value='삭제' onclick='small_delete(<?=$small_code?>)' onfocus='this.blur()' class="btn02_qpass">  </p>


<?} else {?>

      <p style='margin-top:60px'>중분류가 아직 생성되지 않았습니다.</p>
<?}?>

</td> 
    
</tr>  

</table>



<?if($mid_name) {?>

<p align='center' style='margin:30px 0 0 0; font-weight:bold;'><span style='color:blue'>[<?=$big_name?> &gt; <?=$mid_name?>]</span> 소분류 추가</p>

<p align='center'>

<table align='center' width='600' cellspacing='1' cellpadding='5' style='margin: 10px 0 50px 0; background-color:#ccc' border='0'>
<tr style='background-color:white'> 
	  
<td align='center' width='70'>코드</td> 
<td align='center' width=''>분류명</td>
<td align='center' width='70'>목록순서</td>
<td align='center' width='90'>처리</td> 
    
</tr>  


<tr style='background-color:white'> 
	  
<td align='center'><?=$mid_code?> <input type='text' name='small_code_short' size='2' maxlength='2' class='ed' style='text-align:left;' onkeyup='code_check(this, this.form.small_name)' value='<?=$next_code?>'></td> 

<td align='left'><input type='text' name='small_name' maxlength='30' class='ed' style='width:95%; ime-mode:active;' onclick="this.style.color='blue';"></td>

<td align='center'><input type='text' name='view_index' class='ed' style='width:60px; text-align:center;' value='<?=$next_index?>' onclick="this.style.color='blue';"></td>

<td align='center' width='90'>
<input type='button' value="추가하기" onclick="add_check(this.form, '<?=$mid_code?>')" class="btn02_qpass">
</td> 
    
</tr>  

<tr style='background-color:white'><td colspan='4' align='center' style='color:green;'>코드는 칸 안에 10 ~ 99 사이 숫자로 입력하세요.</td></tr>
    
</table>
</p>


<?}?>


</form>


<? include_once("./tail_qpass.php"); ?>

